﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using QuanLyKhachSan.BusinessObject;
using QuanLyKhachSan.DataAccess;
namespace QuanLyKhachSan.BusinessLogic
{
    class DichVuBL
    {
        ConnectData connData = new ConnectData();
        public DataTable LayDSDichVu() {
            string sql = "LayDSDichVu";
            return connData.GetDataTable(sql);
        }

        //Lấy tien
        public static int LayTien(string maDichVu)
        {
            int donGia = 0;
            ConnectData connData1 = new ConnectData();
            SqlConnection conn = connData1.Connect1();
            conn.Open();
            SqlCommand objCmd = new SqlCommand("select DonGia from DichVu where MaDichVu='" + maDichVu + "'", conn);
            SqlDataReader dt = objCmd.ExecuteReader();
            while (dt.Read())
            {
                donGia = Int32.Parse(dt.GetValue(0).ToString());
            }
            conn.Close();
            return donGia;
        }

        //Tim kiem
        public DataTable TimKiemDichVu(string timKiem)
        {
            string sql = "select * from DichVu where TenDichVu like '%" + timKiem + "%' or DonGia like '%" + timKiem + "%'";
            return connData.GetDataTable(sql);
        }

        //thêm mới dịch vụ
        public void ThemDV(DichVuBO dv) {
            
                //string sql = string.Format("INSERT INTO DichVu (MaDichVu, TenDichVu, DonGia)" + "VALUES ('{0}','{1}','{2}')", dv.MaDichVu, dv.TenDichVu, dv.DonGia);
                //if (connData.ExeCuteQuery(sql))
                //{
                //    MessageBox.Show("Thêm mới dịch vụ thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   
                //}

            SqlConnection conn = connData.Connect1();
            conn.Open();
            string sql = "ThemMoiDichVu";
            SqlCommand objCmd = new SqlCommand(sql,conn);
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.Parameters.Add("@MaDichVu", SqlDbType.NVarChar,30);
            objCmd.Parameters.Add("@TenDichVu",SqlDbType.NVarChar,30);
            objCmd.Parameters.Add("@DonGia", SqlDbType.Int);

            objCmd.Parameters["@MaDichVu"].Value = dv.MaDichVu;
            objCmd.Parameters["@TenDichVu"].Value = dv.TenDichVu;
            objCmd.Parameters["@DonGia"].Value = dv.DonGia;
            objCmd.ExecuteNonQuery();
            conn.Close();
          
        }


        //
        public string NextID()
        {
            return Utilities.NextID(connData.GetLastID("DichVu", "MaDichVu"), "DV");
        }

        public bool CheckExists(string madv)
        {
            if (connData.CheckExistValue("DichVu", "MaDichVu", madv))
            {
                return true;
            } return false;
        }


        public void SuaDV(DichVuBO dv) {
           
                // string sql = string.Format("UPDATE DichVu SET TenDichVu = '{0}', DonGia = '{1}' WHERE MaDichVu = '{2}' ", dv.TenDichVu, dv.DonGia, dv.MaDichVu);
                //if (connData.ExeCuteQuery(sql))
                //{
                //    MessageBox.Show("Sửa chức vụ thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                   
                //}
            SqlConnection conn = connData.Connect1();
            conn.Open();
            string sql = "SuaDichVu";
            SqlCommand objCmd = new SqlCommand(sql, conn);
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.Parameters.Add("@MaDichVu", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@TenDichVu", SqlDbType.NVarChar, 30);
            objCmd.Parameters.Add("@DonGia", SqlDbType.Int);

            objCmd.Parameters["@MaDichVu"].Value = dv.MaDichVu;
            objCmd.Parameters["@TenDichVu"].Value = dv.TenDichVu;
            objCmd.Parameters["@DonGia"].Value = dv.DonGia;
            objCmd.ExecuteNonQuery();
            conn.Close();
        }

        //xóa
        public void Delete(string dv)
        {
            string sql = "Delete from DichVu where MaDichVu='" + dv + "'";
            connData.ExeCuteQuery(sql);
        }
    }
}
